package com.example.blogapi;

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        int port = 51184; // 默认端口
        
        // 如果命令行参数中指定了端口，则使用指定的端口
        if (args.length > 0) {
            try {
                port = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                System.err.println("Invalid port number. Using default port " + port);
            }
        }
        
        Server server = new Server(port);
        
        try {
            server.start();
            
            // 添加关闭钩子，确保程序退出时关闭服务器
            Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
            
            System.out.println("Server is running. Press Ctrl+C to stop.");
        } catch (IOException e) {
            System.err.println("Failed to start server: " + e.getMessage());
            e.printStackTrace();
        }
    }
}